<template>
  <div>
      <el-dialog title="自定义流程" :visible.sync="customDialogData.dialogVisible" width="40%" append-to-body>
        <el-form ref="form" :rules="rules" :model="form" label-width="120px">
          <el-form-item label="审批类型" style="width: 100%" prop="tagType">
            <el-select
              v-model="form.tagType"
              placeholder="请选择审批类型"
              style="width: 100%"
            >
              <el-option v-for="(item,index) in tagTypeList" :key="index" :label="item.name" :value="item.name"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="人员选择" prop="per">
            <label slot="label">
               <el-button type="primary" @click="chosseBtn">人员选择</el-button>
            </label>
            <span v-for="(item,index) in perList" :key="index">{{item.userName}}、</span>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm('form')" v-loading.fullscreen.lock="fullscreenLoading"
          >确 定</el-button
          >
          <el-button @click="backBtn">取 消</el-button>
        </span>
      </el-dialog>
    <AllTreeSelect
      v-if="usercomment"
      ref="usercomment"
      @submitfy="submitfy"
    ></AllTreeSelect>
  </div>
</template>
<script>
import {getContractTypeDict,getemPage,submitNdByTemplate,getDeptId} from '@/api/dailyExamine/targetExamine/indicatorFormulation'
import {
    submitZyExamine
} from "@/api/statisticsanalysis/leaderApi.js";
import AllTreeSelect from "@/views/statisticsanalysis/components/alltree-select-zy";
  export default {
    props: {
      customDialogData: {},
    },
    components:{
      AllTreeSelect
    },
    data() {
      const validatePer = (rule, value, callback) => {
        if (this.perList.length < 1) {
          callback(new Error("请选择人员"));
        } else {
          callback();
        }
      };
      return {
        tableData: [],
        form: {},
        categoryList:[],
        perList: [],
        tagTypeList:[
          {name:"会签",value:"会签"},
          {name:"顺序签",value:"会签"},
        ],
        rules: {
          tagType:[{ required: true, message: "请选择审批类型", trigger: "change" }],
          per: [{ required: true, message: "请选择人员",validator: validatePer, }],
        },
        deptId:"",
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        loading:false,
        usercomment: false,
        id:"",
        menuType:"",
        preids:"",
        fullscreenLoading:false
      };
    },
    watch: {
      customDialogData: {
        handler(newVal, oldVal) {
          if (newVal.dialogVisible) {
           //console.log(newVal);
            this.fngetContractTypeDict();
            this.fngetemPage();
            this.menuType = newVal.type
          }
        },
        deep: true,
        immediate: true,
      },
    },
    methods: {
      // 下拉
      fngetContractTypeDict(){
        getContractTypeDict().then((res)=>{
          this.categoryList = res.data.data;
        })
      },
      fngetemPage(){
        this.loading = true;
        var param = {
          current: this.pageIndex,
          size: this.pageSize,
          menuType:this.menuType,
          tagName:"业绩合同"
        }
        getemPage(param).then((res)=>{
          this.tableData = res.data.data.records;
          this.totalPage = res.data.data.total;
          this.loading = false;
        })
      },
      // 新增弹窗
      newBtn() {
        this.dialogVisible = true;
        this.form = {};
        this.perList = [];
        this.fngetDeptId();
        this.id = "";
      },
      //人员选择
      chosseBtn(){
        this.usercomment=true;
        this.jsonData = JSON.stringify(this.perList);
        this.$nextTick(() => {
          this.$refs.usercomment.init("12", "sy", this.jsonData, "htcj");
        });
      },
      // 获取部门deptId
      fngetDeptId(){
        getDeptId().then((res)=>{
         //console.log(res.data)
          this.deptId = res.data.data
        })
      },
      submitForm(formName) {
          var text = ""
          if(this.form.tagType == '会签'){
            text = '并行'
          }else{
            text = '串行'
          }
          var params = {
            ldsh:text,
            sonhq:this.preids,
            rows:this.customDialogData.rows,
          }

        this.$refs[formName].validate((valid) => {
          if (valid) {
              this.fullscreenLoading = true;
              submitZyExamine(params).then((res)=>{
                  this.$message.closeAll();
                  this.$message.success("提交成功");
                  this.fullscreenLoading = false;
                  this.backBtn()
              })

          } else {
           //console.log("error submit!!");
            return false;
          }
        });
      },
      backBtn(){
        this.form = {};
        this.perList = [];
        this.fngetemPage();
        this.id = "";
        this.customDialogData.dialogVisible = false
        this.$emit("customDialogChildren",'返回')
      },
      submitfy(data) {
        this.perList = data.tableDataUser
        this.perList.forEach(ele=>{
          this.preids=this.preids+ele.userId+","
        })
        // if (data.type == "fy") {
        //   this.fayuerendataList = data.tableDataUser;
        // } else if (data.type == "sy") {
        //   this.shouyuerendataList = data.tableDataUser;
        // } else if (data.type == "jg") {
        //   this.jigoudataList = data.tableDataUser;
        // }
        console.log(data)
      },

    },
  };
</script>
